/**
 * 作者: wgx
 * 日期: 2024-12-03 16:14:36
 * 备注: 用户物资争夺模块数据
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.Common.PBUserSnatchInfo;
import com.motu.monstercity.protocol.Common.PBUserSnatchRecord;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.commondata.bag.ItemId;
import com.motu.monstercity.server.game.commondata.maintask.MainTaskConstant;
import com.motu.monstercity.server.game.log.MotuLogManager;
import com.motu.monstercity.server.game.logic.FightManager;
import com.motu.monstercity.server.game.logic.GameUser;
import com.motu.monstercity.server.game.logic.MainTaskManager;
import com.motu.monstercity.server.game.logic.RankActManager;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

public class UserSnatchInfo extends UserSnatchInfoModel {
    public UserSnatchInfo() {
        super();
    }

    public JsonObject insert() {
        putResetTimeDay(TimeUtils.getCurTime());
        return super.insert();
    }

    public void reset() {
        boolean isUpdate = false;
        long curTime = TimeUtils.getCurTime();
        if (!TimeUtils.isToday(getResetTimeDay())) {
            putOfferFightNum(0);
            putLastEnemyid(0);
            putResetTimeDay(curTime);
            isUpdate = true;
        }

        if (curTime - getProtectTime() >= AllParam.SNATHC_PROTECT[1] * TimeUtils.HOUR) {// 超过6小时,去掉保护机制
            putIsProtect(0);
            putProtectTime(0);
            isUpdate = true;
        }
        if (getTransferTime() == 0) {
            putTransferTime(TimeUtils.getCurTime());
            isUpdate = true;
        }
        if (isUpdate) {
            update();
        }
    }

    public JsonArray takePartnerIds() {
        if (Tool.isEmpty(getPartnerIds())) {
            return new JsonArray();
        } else {
            return new JsonArray(getPartnerIds());
        }
    }

    // 记录日志并增加贸易币
    public void addMoney(UserInfo userInfo, int num, Object... params) {
        long balance = getMoney();// 当前余额
        long dbid = 0;
        MotuLogManager.logItemIncome(userInfo, ItemId.FIGHT_SNATCH_MONEY, dbid, balance, num, params);
        super.addMoney(num);
    }

    public void subMoney(UserInfo userInfo, int num, Object... params) {
        long balance = getMoney();// 当前余额
        if (num > balance) {
            return;// 余额不足
        }
        long dbid = 0;
        MotuLogManager.logItemConsume(userInfo, ItemId.FIGHT_SNATCH_MONEY, dbid, balance, num, params);
        super.addMoney(-num);
    }


    // 物资争夺单场达到{0}连胜
    public void updateMaxKoNum(PBUserData.Builder pbUserData, UserInfo userInfo, int koNum) {
        if (koNum > getMaxKonum()) {
            super.putMaxKonum(koNum);
            MainTaskManager.updateTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_121, koNum);// 更新主线任务,成就,日常任务的进度
            RankActManager.updateTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_121, koNum);
        }
    }

    // 被打的时候是否触发保机制
    public void checkSnatchProtect() {
        long curTime = TimeUtils.getCurTime();
        if (CommonUtils.isSameHour(curTime, getDefendTime())) {
            super.addDefendNum(1);
        } else {
            super.putDefendNum(1);
            super.putDefendTime(curTime);
        }
        if (getDefendNum() >= AllParam.SNATHC_PROTECT[0]) {
            putIsProtect(1);
            putProtectTime(curTime);
        }
    }

    public PBUserSnatchInfo.Builder takeInitBuilder() {
        PBUserSnatchInfo.Builder sb = super.takeInitBuilder();
        if (getLastEnemyid() > 0) {
            UserInfo userInfo = GameUser.getUserInfo(getId());
            ServerSnatchFame serverSnatchFame = FightManager.getServerSnatchFame(userInfo.getServerId(), getLastEnemyid());
            if (serverSnatchFame != null) {
                UserShow userShow = GameUser.getUserShow(serverSnatchFame.getAttactId());
                if (userShow != null) {
                    sb.setLastEnemyInfo(userShow.takeInitBuilder());
                }
            }
        }

        return sb;
    }

    public PBUserSnatchInfo.Builder takeBuilder() {
        PBUserSnatchInfo.Builder sb = super.takeBuilder();
        if (getLastEnemyid() > 0) {
            UserInfo userInfo = GameUser.getUserInfo(getId());
            ServerSnatchFame serverSnatchFame = FightManager.getServerSnatchFame(userInfo.getServerId(), getLastEnemyid());
            if (serverSnatchFame != null) {
                UserShow userShow = GameUser.getUserShow(serverSnatchFame.getAttactId());
                if (userShow != null) {
                    sb.setLastEnemyInfo(userShow.takeInitBuilder());
                }
            }
        }

        return sb;
    }
}
